<?php

class bannerdetail extends Controller
{
	private  $_template_f;  
	function __construct()
	{
		parent::Controller();
		$this->_lang = $this->mycommon->getLangCode();
		$this->load->language('common', $this->_lang);
		$common_lang = $this->lang->line('common_lang');
		if(!$this->session->userdata('uname'))
		{
			redirect(site_url('login/'));
			die();
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > -1)
			{
				show_error($common_lang['access_denied']);
				die();
			}
		}
		$this->_template_f = $this->config->item('template_f');
		$this->load->language('bannerdetail/bannerdetail', $this->_lang);
		$this->load->helper('breadcrumb');
        $this->load->model('bannerdetail/bannerdetail_model');
        $this->load->library('mychart');
	}
	
	function _loadHeader($title = '')
	{
		$header = array();
		$header['title'] = $title == '' ? 'Self serving' : $title;
		$header['template_f'] = $this->_template_f;
		
		$header['username'] = '';
		$header['userid'] = '';
		$header['permitid'] = '';
		$header['module_name'] = 'bannerdetail';
        $header['lang'] = $this->_lang;
        
		if($this->session->userdata('uname'))
		{
			$header['username'] = $this->session->userdata('uname');
			$header['userid'] = $this->session->userdata('uid');
			$header['permitid'] = $this->session->userdata('permitid');
		}
        $header['common_lang'] = $this->lang->line('common_lang');
		
		// load header
		$this->load->view($this->_template_f . 'preheader_view');
		$this->load->view($this->_template_f . 'header_view', $header);
	}
	
	function _loadFooter()
	{
		$this->load->view($this->_template_f . 'footer_view');
	}
    
   	function _loadMenu()
	{
		$data = array();
		$data['username'] = '';
		$data['userid'] = '';
		$data['permitid'] = '';
		$data['lang'] = $this->_lang;
		if($this->session->userdata('uname'))
		{
			$data['username'] = $this->session->userdata('uname');
			$data['userid'] = $this->session->userdata('uid');
			$data['permitid'] = $this->session->userdata('permitid');
		}
		$data['common_lang'] = $this->lang->line('common_lang');
		
		$this->load->view($this->_template_f . 'menu_view', $data);
	}
    
    function index()
    {
        if(!$this->session->userdata('uname')){
			$currUrl = $this->mycommon->getCurrentUrl();
			redirect(site_url('/login'). '/?url=' . urlencode($currUrl));
			die();
		}
        $bannerdetail_lang = $this->lang->line('bannerdetail_lang');
        
        $data['bannerdetailid']=$bannerdetailid=$this->input->get('bannerid');
        $data['fromdate']=$fromdate=$this->input->get('fromdate');
        $data['todate']=$todate=$this->input->get('todate');
        
        
        $bannerdetailinfoTemp=$this->bannerdetail_model->selfserving_get_banner_info($bannerdetailid);
        $data['info']=$bannerdetailinfoTemp['info'];
        
        $user_id = $this->session->userdata('uid');
		$group_id = $this->session->userdata('groupid');
		$permit_id = $this->session->userdata('permitid');
        $chkBanner = check_permit_id($user_id,$permit_id,$group_id,$bannerdetailid,1);
        $chkFromDate = is_date($fromdate);
        $chkToDate   = is_date($todate);
        
        if(($chkBanner==false)||($chkFromDate==false)||($chkToDate==false)||($fromdate>$todate))
        {
            redirect(site_url('banner/error'));
            die();
        }else{ 
            //load header
            $this->_loadHeader($bannerdetail_lang['bannerdetail']);
            //load left menu
            $this->_loadMenu();
            $data['category']=$bannerdetailinfoTemp['category'];
            //2nd table
            $temp=$this->bannerdetail_model->selfserving_get_all_by_banner($bannerdetailid,$fromdate,$todate);
            $data['bannerdetail'] = $temp['banner'];
            $data['total'] = $temp['total'];
            $type=0;
            $data['bannerdetail_lang'] = $bannerdetail_lang;
            $this->load->view($this->_template_f.'bannerdetail/bannerdetail_view',$data);
            $this->_loadFooter();
        }
    }
    function click_chart()
    {
        if(!$this->session->userdata('uname')){
			echo 'unlogin';
			die();
		}
        $bannerdetailid=$this->input->get('bannerid');
        $user_id = $this->session->userdata('uid');
		$group_id = $this->session->userdata('groupid');
		$permit_id = $this->session->userdata('permitid');
        $chkBanner = check_permit_id($user_id,$permit_id,$group_id,$bannerdetailid,1);
        
        $fromdate=date('Y-m-d',strtotime($this->input->get('fromdate')));
        $todate=date('Y-m-d',strtotime($this->input->get('todate')));
        $chkFromDate = is_date($fromdate);
        $chkToDate   = is_date($todate);
        
        $type = is_numeric($this->input->get('type'))? $this->input->get('type') : 0;
        $chkType= (($type!=0)&&($type!=1)) ? false : true;
        
        if(($chkBanner==false)||($chkFromDate==false)||($chkToDate==false)||($chkType==false))
        {
            echo 'error';
            die();
        }else{ 
            $data = $this->chart_data($bannerdetailid,$fromdate,$todate,$type);
            $data['bannerdetail_lang']=$this->lang->line('bannerdetail_lang');
            $this->load->view($this->_template_f.'campaign/chart_view',$data);
        }
    }
    
    function chart_data($bannerdetailid,$fromdate,$todate,$type)
    {
        $data = array();
        $bannerdetail_lang = $this->lang->line('bannerdetail_lang');
        if($fromdate == $todate)
        {
            $curdate=$fromdate;
            $chartdata = $this->bannerdetail_model->selfserving_get_banner_chart_hour($bannerdetailid,$curdate,$type);
            $arrData=$this->mychart->createDataHour($curdate,$chartdata['ban1']);
            $label1=$chartdata['ban2'][0]['bannername'];
            $data['type']= ($type==0)?'Click':$bannerdetail_lang['impressions'];
            $data['data']=json_encode(array('name'=>strip_tags($label1),'data'=>$arrData['data'],'color'=>'#627AAD'));
            $data['date']=json_encode($arrData['hour']);
            return $data;              
        }else{
            $chartdata=$this->bannerdetail_model->selfserving_get_banner_chart($bannerdetailid,$fromdate,$todate,$type);
            $arrDate=$this->mychart->createDateRangeArray($fromdate,$todate);
            $arrData=$this->mychart->createData($arrDate,$fromdate,$chartdata['ban1']);
            $label1=$chartdata['ban2'][0]['bannername'];
            $data['type']= ($type==0)?'Click':$bannerdetail_lang['impressions'];
            $data['data']=json_encode(array('name'=>strip_tags($label1),'data'=>$arrData,'color'=>'#627AAD'));
            $data['date']=json_encode($arrDate);
            $data['step'] =(count($arrDate)<=10)?1:((count($arrDate)<=14)?2:floor(count($arrDate)/7));
            return $data; 
        }
    }
    
    function timeFilter()
    {
        $this->config->load('config_custom');
        $data['price'] = $this->config->item('cpc_price');
        
        $data['bannerdetailid']=$bannerdetailid=$this->input->get('bannerid');
        $data['fromdate']=$fromdate=date('Y-m-d',strtotime($this->input->get('fromdate')));
        $data['todate']=$todate=date('Y-m-d',strtotime($this->input->get('todate')));
        $data['type']=$type=$this->input->get('type');
        $bannerdetailinfoTemp=$this->bannerdetail_model->selfserving_get_banner_info($bannerdetailid);
        $data['info']=$bannerdetailinfoTemp['info'];

        $data['category']=$this->bannerdetail_model->selfserving_get_all_categories();

        $temp=$this->bannerdetail_model->selfserving_get_all_by_banner($bannerdetailid,$fromdate,$todate);
        $data['bannerdetail'] = $temp['banner'];
        $data['total'] = $temp['total'];
        $data['bannerdetail_lang']=$this->lang->line('bannerdetail_lang');
        $this->load->view($this->_template_f.'bannerdetail/time_view',$data);
    }
    function export()
    {
        $data['bannerid'] = $bannerdetailid = $this->input->get('bannerid');
        $data['fromdate'] = $fromdate = date('Y-m-d',strtotime($this->input->get('fromdate')));
        $data['todate'] = $todate = date('Y-m-d',strtotime($this->input->get('todate')));
        $data['type'] = $type = $this->input->get('type');
        $data['price'] = $this->config->item('cpc_price');
        
        $bannerdetailinfoTemp=$this->bannerdetail_model->selfserving_get_banner_info($bannerdetailid);
        $data['info']=$bannerdetailinfoTemp['info'];
        $data['category']=$bannerdetailinfoTemp['category'];
        
        $temp=$this->bannerdetail_model->selfserving_get_all_by_banner($bannerdetailid,$fromdate,$todate);
        $data['bannerdetail'] = $temp['banner'];
        $data['total'] = $temp['total'];
        $data['bannerdetail_lang']=$this->lang->line('bannerdetail_lang');
        
        if($data['type'] != 0) $this->load->view($this->_template_f.'bannerdetail/export',$data,true);
    	else	//Export PDF
        {
        	$this->load->library('pdfreport');
        	
        	$html = $this->load->view($this->_template_f . 'bannerdetail/bannerdetail_pdf_view',$data,true);
        	$this->load->library('myvietstring');
        	$pdf_name = 'selfserving_'.$this->session->userdata('uname').'_'.str_replace(' ', '_', $this->myvietstring->removeVietChars(strip_tags($data['info'][0]['bannername']))).'_'.date('d.m.Y').'.pdf';
        	$this->pdfreport->html2pdf($html,$pdf_name);
        }
    }
    function edit()
    {
        $val = $this->input->post('ns');
        $bannerid = $this->input->get('bannerid');
        $type = $this->input->get('type');
        $count = $this->bannerdetail_model->selfserving_banner_update_inline($bannerid,$val,$type);
        if($count==true)
        {
            if($type==1 && $val=='1'){
                echo '<img src="/images/imgrun.gif" style="vertical-align: middle;"/><span class="ver-middle"> Đang chạy </span>';
            }elseif($type==1 && $val=='2')
            {
                echo '<img src="/images/imgpause.gif" style="vertical-align: middle;"/><span class="ver-middle"> Tạm dừng </span>';
            }else{
                echo $val;
            }
        }else{
            echo 'Error,try again,please!';
        }        
    }
    function trackBanner()
    {
        $bannerdetail_lang = $this->lang->line('bannerdetail_lang');
        //header
        $this->_loadHeader($bannerdetail_lang['trackbanner']);
        $this->_loadMenu();
        //main
        $data['fromdate'] = date('Y-m-d',strtotime($this->input->get('fromdate')));
        $data['todate'] = date('Y-m-d',strtotime($this->input->get('todate')));
        
        $data['page'] = $page = $this->input->get('page') ? $this->input->get('page') : '1';
        $data['numrow'] = $numrow = $this->input->get('numrow') ? $this->input->get('numrow') : '20';
        
        $data['bannerid'] = $bannerid = $this->input->get('bannerid');
        $data['bannerdetail_lang'] = $bannerdetail_lang;
        $temp = $this->bannerdetail_model->selfserving_get_banner_info($bannerid);
        $data['info'] = $temp['info'];
        $data['location']=$location = isset($_COOKIE["__R"]) ? $_COOKIE["__R"]:0;
        $lstzoneTemp=$this->bannerdetail_model->selfserving_admin_get_lstzone_tracking_by_banner($bannerid,$location);
        $lstzone=$lstzoneTemp['ban1'][0]['_lstzoneid'];
        $temp = $this->bannerdetail_model->selfserving_get_url_by_bannerid($page,$numrow,$lstzone);
        $data['url'] = $temp['url'];
        $data['totalpage'] = $temp['totalpage'][0]['total'];
        $data['totalurl'] = $temp['totalpage'][0]['totalurl'];

        $this->load->view($this->_template_f.'bannerdetail/bannertracking_view',$data);
        $this->_loadFooter();
    }
    function ajax_trackBanner()
    {
        $data['bannerdetail_lang'] = $this->lang->line('bannerdetail_lang');;
        $data['page'] = $page = $this->input->get('page') ? $this->input->get('page') : '1';
        $data['numrow'] = $numrow = $this->input->get('numrow') ? $this->input->get('numrow') : '20';
        
        $data['bannerid'] = $bannerid = $this->input->get('bannerid');
        $data['location']=$location = isset($_COOKIE["__R"]) ? $_COOKIE["__R"]:0;
        $lstzoneTemp=$this->bannerdetail_model->selfserving_admin_get_lstzone_tracking_by_banner($bannerid,$location);
        $lstzone=$lstzoneTemp['ban1'][0]['_lstzoneid'];
        $temp = $this->bannerdetail_model->selfserving_get_url_by_bannerid($page,$numrow,$lstzone);
        $data['url'] = $temp['url'];
        $data['totalpage'] = $temp['totalpage'][0]['total'];
        $data['totalurl'] = $temp['totalpage'][0]['totalurl'];

        $this->load->view($this->_template_f.'bannerdetail/ajax_bannertracking_view',$data);
    }
    function urltracking()
    {
        $url = $this->input->get('url');
        $zoneid = $this->input->get('zoneid');
        $domain = $this->input->get('domain');
        $bannerid = $this->input->get('bannerid');
        $data = $this->bannerdetail_model->selfserving_get_url_trackingbanner($url,$domain,$bannerid,$zoneid);
        $a=$data[0]['url'];
        $b=explode('?',$a);        
        
        redirect(site_url('bannerdetail/redirect?ref='.$b[0].(isset($b[1])?'&'.$b[1]:'')));    
        die();
    }
    function redirect(){
        $a = $this->input->get('ref');        
        $html="function redirect(url) {if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
                                var referLink = document.createElement('a');                                referLink.href = url;
                                document.body.appendChild(referLink);
                                referLink.click();
                        } else {
                                location.href = url;
                        }
                }";
        //echo('<script language="javascript">top.location="'.$a.'";</script>');
        echo('<html xmlns="http://www.w3.org/1999/xhtml"><header></header><body></body></html>');
        echo('<script language="javascript">'.$html.'redirect("'.$a.'");</script>');
    }
}
?>